#pragma once

#include "Base/Tests/Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestDisplayRotation
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
public:
	typedef SHARED_POINTER_TYPE( APP_NAME )		TestDisplayRotationPtr;


	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{
		ESS()->GetEventSystem()->AttachEventListener(
			DelegateBuilder::Create( TestDisplayRotationPtr(this), &APP_NAME::_OnWindowEvent ),	ESystemEvent::WINDOW );
	}
	
	~APP_NAME ()
	{
		ESS()->GetEventSystem()->DetachEventListener(
			DelegateBuilder::Create( TestDisplayRotationPtr(this), &APP_NAME::_OnWindowEvent ),	ESystemEvent::WINDOW );
	}

	void Update (float dt) override
	{
		TestApp::Update( dt );

		if ( ESS()->GetInput()->IsKeyClicked( EKey::R ) or
			 ESS()->GetInput()->GetTouch(0).IsClicked() )
		{
			Display::EOrientation orient = GetPlatform()->GetDisplay().Orientation();

			orient = Display::EOrientation( (orient+1) % Display::_ORIENTS_COUNT );

			EDisplayOrientation::type new_orient = EDisplayOrientation::FromOrientation( orient );

			GetPlatform()->SetDisplayOrientation( new_orient );
		}
	}

	void OnEnter () override
	{
	}
	
	void _OnWindowEvent (const SysEvent &ev)
	{
		if ( ev.window.type == SysEvent::Window::ORIENTATION_CHANGED )
		{
			LOG( (string("orientation changed to ") << ev.window.orientation).cstr(), ELog::INFO );
		}
		else
		if ( ev.window.type == SysEvent::Window::RESIZED )
		{
			LOG( (string("screen resized ") << ev.window.width << "x" << ev.window.height).cstr(), ELog::INFO );
		}
	}
};